#!/bin/bash
# install pi-battery-widget

if [ `whoami` = root ]; then
  echo "Please run as the user who has started the desktop, not as root"
  exit 1
fi


# kill any running pi-battery-widget command so that it can be overwritten
pkill pi-battery- 2>/dev/null

# create bin directory in ~, if it does not exist
[ ! -d ~/bin  ] && mkdir ~/bin

#make sure that files are executable
chmod +x pi-battery-widget

#copy necessary files to ~/bin
cp battery_icon.png ~/bin
cp pi-battery-widget ~/bin
cp pi-battery-reader.py ~/bin

# add line to lxsession autostart, if it does not already exist
if ! grep -q "pi-battery-widget" ~/.config/lxsession/Lubuntu/autostart
then
    cp ~/.config/lxsession/Lubuntu/autostart ./lxsession_autostart_backup
    echo "@pi-battery-widget > /dev/null &" >> ~/.config/lxsession/Lubuntu/autostart
fi

#restart gtk_battery in background making sure that it lives on if calling console is closed
nohup ~/bin/pi-battery-widget > /dev/null 2>/dev/null &
